package com.spoto.oa.servlet;

import java.io.IOException;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.spoto.oa.model.Notice;
import com.spoto.oa.model.NoticeType;
import com.spoto.oa.service.NoticeServ;
import com.spoto.oa.service.impl.NoticeServImpl;
import com.spoto.oa.util.DateUtil;

public class NoticeServlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public NoticeServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		
		NoticeServ noticeServ=new NoticeServImpl();
		Notice notice=new Notice();
		String type=request.getParameter("op_type");
		List<Notice> notices=new ArrayList<Notice>();
		if("to_add".equals(type)){// 
			List<NoticeType> noticeTypes=noticeServ.getAllNoticeTypes();
			request.setAttribute("noticeTypes", noticeTypes);
			request.getRequestDispatcher("/notice/add_notice.jsp").forward(request, response);
		}else if("add".equals(type)){// 
			String title=request.getParameter("title");
			String noticeType=request.getParameter("type_select");
			String content=request.getParameter("content");
			HttpSession session=request.getSession();
			String username=(String) session.getAttribute("username");
			  
			SimpleDateFormat spdf=new SimpleDateFormat("yyyy-MM-dd");
			java.sql.Date create_date=DateUtil.parseToDate(spdf.format(new java.util.Date()));
			notice.setTitle(title);
			notice.setCreator(username);
			notice.setCreate_date(create_date);
			notice.setContent(content);
			notice.setType(noticeType);
			try {
				noticeServ.saveNotice(notice);
			} catch (SQLException e) {
				e.printStackTrace();
			}	
			List<NoticeType> noticeTypes=noticeServ.getAllNoticeTypes();
			request.setAttribute("noticeTypes", noticeTypes);
			notices=noticeServ.getAllNotices();
			request.setAttribute("notices", notices);
			request.getRequestDispatcher("/notice/welcome_notice.jsp?op_type=to_list").forward(request, response);
		}else if("to_list".equals(type)){// 
			List<NoticeType> noticeTypes=noticeServ.getAllNoticeTypes();
			request.setAttribute("noticeTypes", noticeTypes);
			notices=noticeServ.getAllNotices();
			request.setAttribute("notices", notices);
			request.getRequestDispatcher("/notice/welcome_notice.jsp").forward(request, response);
			
		}else if("delete".equals(type)){// 
			int id = Integer.parseInt(request.getParameter("id"));
			noticeServ.deleteById(id);
			
			List<NoticeType> noticeTypes=noticeServ.getAllNoticeTypes();
			request.setAttribute("noticeTypes", noticeTypes);
			notices=noticeServ.getAllNotices();
			request.setAttribute("notices", notices);
			request.getRequestDispatcher("/notice/welcome_notice.jsp?op_type=to_list").forward(request, response);
		}else if ("to_update".equals(type)){
			int id = Integer.parseInt(request.getParameter("id"));
			notice = noticeServ.getById(id);
			request.setAttribute("notice", notice);
			List<NoticeType> noticeTypes=noticeServ.getAllNoticeTypes();
			request.setAttribute("noticeTypes", noticeTypes);
				request.getRequestDispatcher("/notice/update_notice.jsp")
				.forward(request, response);
		}else if ("update".equals(type)) {
			int id=Integer.parseInt(request.getParameter("id"));
			String title=request.getParameter("title");
			String noticeType=request.getParameter("type_select");
			String content=request.getParameter("content");
			HttpSession session=request.getSession();
			String username=(String) session.getAttribute("username");
			System.out.println(title);
			SimpleDateFormat spdf=new SimpleDateFormat("yyyy-MM-dd");
			java.sql.Date create_date=DateUtil.parseToDate(spdf.format(new java.util.Date()));
			notice.setId(id);
			notice.setTitle(title);
			notice.setCreator(username);
			notice.setCreate_date(create_date);
			notice.setContent(content);
			notice.setType(noticeType);
			noticeServ.updateNotice(notice);
			
			List<NoticeType> noticeTypes=noticeServ.getAllNoticeTypes();
			request.setAttribute("noticeTypes", noticeTypes);
			notices=noticeServ.getAllNotices();
			request.setAttribute("notices", notices);
			request.getRequestDispatcher("/notice/welcome_notice.jsp?op_type=to_list").forward(request, response);
		}if ("search".equals(type)) {
			String searchType = request.getParameter("searchType");
			String infor =request.getParameter("infor");
			if (null==searchType) {
				notices=noticeServ.getAllNotices();
			}else if("".equals(infor)){
				notices=noticeServ.searchAllByType(searchType);
			}else{
				notices = noticeServ.searchAllByTypeAndTitle(searchType,infor);
			}		
			request.setAttribute("notices", notices);
			request.setAttribute("infor", infor);
			request.setAttribute("searchType", searchType);
			List<NoticeType> noticeTypes=noticeServ.getAllNoticeTypes();
			request.setAttribute("noticeTypes", noticeTypes);
			request.getRequestDispatcher("/notice/welcome_notice.jsp?op_type=to_list").forward(request, response);
		}else if (("multi_del").equals(type)) {
				String[] delIds = request.getParameterValues("del-flag");
				for (int i = 0; i < delIds.length; i++) {
					int id=Integer.parseInt(delIds[i]);
					noticeServ.deleteById(id);
				}
				List<NoticeType> noticeTypes=noticeServ.getAllNoticeTypes();
				request.setAttribute("noticeTypes", noticeTypes);
				notices=noticeServ.getAllNotices();
				request.setAttribute("notices", notices);
				request.getRequestDispatcher("/notice/welcome_notice.jsp?op_type=to_list").forward(request, response);
			}
			
		
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
